package com.example.itemdecorationdemo.itemDecoration

import android.content.Context
import android.graphics.*
import com.example.itemdecorationdemo.itemDecoration.SectionDecoration.DecorationCallback
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import android.text.TextPaint
import androidx.recyclerview.widget.RecyclerView
import android.text.TextUtils
import com.example.itemdecorationdemo.R
import android.util.Log
import android.view.View

class `TestItemDecoration-Backuo1`(context: Context) : ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
//        super.getItemOffsets(outRect, view, parent, state)
//        val position = parent.getChildAdapterPosition(view)
//        val d = 40
//        when (position % 4) {
//            1 -> outRect.top = d
//            0 -> outRect.left = d
//            2 -> outRect.right = d
//            else -> outRect.bottom = d
//        }
//
//        outRect.set(d, d, d, d)
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        Log.d(TAG, "onDraw parent:$parent c[width,height]=[${c.width},${c.height}]")
        //onDraw parent:androidx.recyclerview.widget.RecyclerView{75ac334 VFED..... ........ 0,0-1200,2313 #7f0800f2 app:id/simple_fragment_rv} c[width,height]=[1200,2313]

        //画一个地图矩形，围绕整个RecyclerView
        //c.drawRect(0F, 0F, c.width.toFloat(), c.height.toFloat(), Paint())
        //
//        val paint =  Paint()
//        paint.style = Paint.Style.STROKE; //空心矩形框
//        //paint.style = Paint.Style.FILL//实心矩形框
//        paint.color = Color.RED
//        paint.strokeWidth = 10F
//        c.drawRect(0F, 0F, c.width.toFloat(), c.height.toFloat(), paint)
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
        drawTop(c,parent,state)
        drawBottom(c,parent,state)

    }

    private fun drawTop(c: Canvas, parent: RecyclerView, state: RecyclerView.State){
        val paint: Paint = Paint()
        //paint.style = Paint.Style.STROKE; //空心矩形框
        paint.style = Paint.Style.FILL//实心矩形框
        paint.color = Color.RED
        paint.strokeWidth = 10F
        c.drawRect(0F, 0F, c.width.toFloat(), 200F, paint)

        val textPaint = Paint()
        textPaint.color = Color.BLACK
        textPaint.strokeWidth = 12F
        textPaint.textSize = 72F

        c.drawText("顶部悬浮title", 0F, 180F, textPaint)
    }

    private fun drawBottom(c: Canvas, parent: RecyclerView, state: RecyclerView.State){
        val paint = Paint()
        //paint.style = Paint.Style.STROKE; //空心矩形框
        paint.style = Paint.Style.FILL//实心矩形框
        paint.color = Color.RED
        paint.strokeWidth = 10F
        c.drawRect(0F, c.height.toFloat() - 200F, c.width.toFloat(), c.height.toFloat(), paint)

        val textPaint = Paint()
        textPaint.color = Color.BLACK
        textPaint.strokeWidth = 12F
        textPaint.textSize = 72F

        c.drawText("底部悬浮title", 0F, c.height.toFloat() - 20F, textPaint)
    }

    companion object {
        private const val TAG = "TestItemDecoration"
    }
}